package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.DistributionMember;
import com.addplus.townmall.server.api.model.town.expand.DistributionMemberExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 分销会员表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface DistributionMemberMapper extends BaseAddMapper<DistributionMember> {

    @Select("SELECT dm.id,m.nick_name,m.head_portrait,dg.grade_name,dm.member_id," +
            "dm.superior_member_id,dm.grade_id,dm.balance,dm.settlement_amount," +
            "dm.withdrawals_amount,dm.application_amount,dm.waiting_amount,dm.invalid_amount," +
            "dm.freeze_amount,dm.receipt_amount,dm.sale_amount " +
            "FROM distribution_member dm " +
            "LEFT JOIN distribution_grade dg ON dg.id = dm.grade_id AND dg.is_delete = 0 " +
            "LEFT JOIN member m ON m.id = dm.member_id " +
            "WHERE dm.member_id = #{memberId} AND dm.is_delete = 0")
    DistributionMemberExpand getDistributionMemberInfornation(@Param("memberId") Integer memberId);

    @Select("select * from distribution_member where member_id =#{memberId}")
    DistributionMember getDistributionMemberByMemberId(@Param("memberId") Integer memberId);


    @Select("SELECT dm.id,dm.member_id,m.nick_name,m.head_portrait,dm.order_count " +
            "FROM distribution_member dm " +
            "LEFT JOIN member m ON m.id = dm.member_id AND dm.is_delete = 0 " +
            "WHERE dm.superior_member_id = #{memberId} ORDER BY dm.gmt_create")
    IPage<DistributionMemberExpand> getSubMemberByPage(Page<DistributionMemberExpand> page, @Param("memberId") Integer memberId);

    @Select("SELECT\n" +
            "\tm.nick_name,\n" +
            "\tsupm.nick_name superior_member_name,\n" +
            "\tdg.grade_name,\n" +
            "\tdm.* \n" +
            "FROM\n" +
            "\tdistribution_member dm\n" +
            "\tLEFT JOIN member m ON m.id = dm.member_id\n" +
            "\tLEFT JOIN distribution_grade dg ON dm.grade_id = dg.id\n" +
            "\tLEFT JOIN member supm ON dm.superior_member_id = supm.id \n" +
            "WHERE\n" +
            "\tdm.is_delete = 0")
    List<DistributionMemberExpand> getMemberExpandByPage(Page page);
}
